<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\Sms;
use app\common\UserService;
use app\model\Bank;
use app\model\Config;
use app\model\User;
use app\model\UserBank;
use app\model\UserWithdraw;
use app\tool\Random;

class BankController extends BaseController
{
	#银行
	function bank()
	{
		$rows = Bank::select();
		$this->writeJson($rows);
	}
	
	#获取提现信息
	function getWithdrawConfig()
	{
		$user = User::find($this->user_id);
		if (!$user) {
			$this->writeJson(401, '请先登录');
		}
		$withdraw_num = Config::getFieldByName('withdraw_num', 'value');
		$withdraw_amount = Config::getFieldByName('withdraw_amount', 'value');
		$withdraw_rate = Config::getFieldByName('withdraw_rate', 'value');
		$amount = UserWithdraw::where(['user_id' => $this->user_id, ['status', 'in', [1, 2]]])->whereDay('createtime')->sum('amount'); //当天提现金额
		$num = UserWithdraw::where(['user_id' => $this->user_id, ['status', 'in', [1, 2]]])->whereDay('createtime')->count(); //当天提现次数
		$this->writeJson(['use_num' => $num,'num'=>(int)$withdraw_num, 'last_amount' => $withdraw_amount - $amount, 'rate' => $withdraw_rate, 'money' => $user->money]);
	}
	
	/**
	 * 添加银行卡
	 * @return void
	 */
	function addBank()
	{
		$name = $this->request->post('name');
		$account = $this->request->post('account');
		$image = $this->request->post('image');
		$bank = $this->request->post('bank');
		$bank_name = $this->request->post('bank_name');
		$mobile = $this->request->post('mobile');
		$captcha = $this->request->post('captcha');
		if (empty($name) || empty($account) || empty($bank) || empty($bank_name) || empty($mobile)|| empty($captcha)|| empty($image)) {
			$this->writeJson(0, '参数错误');
		}
		$captchaResult = Sms::check($mobile, $captcha, 'validatemobile');
		if (!$captchaResult) {
			$this->writeJson(0,'验证码错误');
		}
		UserBank::create([
			'user_id' => $this->user_id,
			'name' => $name,
			'image' => $image,
			'account' => $account,
			'bank' => $bank,
			'bank_name' => $bank_name,
			'mobile' => $mobile,
		]);
		$this->writeJson();
	}
	
	/**
	 * 删除银行卡
	 * @return void
	 */
	function deleteBank()
	{
		$bank_id = $this->request->post('bank_id/d');
		$bank = UserBank::findOrFail($bank_id);
		$bank->delete();
		$this->writeJson();
	}
	
	#提现
	function withdraw()
	{
		$bank_id = $this->request->post('bank_id/d');
		$amount = $this->request->post('amount');
		$bank = UserBank::findOrFail($bank_id);
		$user = User::find($this->user_id);
		if (!$user) {
			$this->writeJson(401, '请先登录');
		}
		$withdraw_rate = Config::getFieldByName('withdraw_rate', 'value');
		$withdraw_amount = Config::getFieldByName('withdraw_amount', 'value');
		$withdraw_num = Config::getFieldByName('withdraw_num', 'value');
		$today_amount = UserWithdraw::where(['user_id' => $this->user_id, ['status', 'in', [1, 2]]])->whereDay('createtime')->sum('amount'); //当天提现金额
		$today_num = UserWithdraw::where(['user_id' => $this->user_id, ['status', 'in', [1, 2]]])->whereDay('createtime')->count(); //当天提现次数
		if ($today_num >= $withdraw_num) {
			$this->writeJson(0, '今日提现次数已到上限');
		}
		if (bcadd((string)$today_amount,$amount,2) > $withdraw_amount) {
			$this->writeJson(0, '今日提现金额已到上限');
		}
		$fee = $amount * $withdraw_rate/100;
		$real_amount = $amount - $fee;
		
		try {
			UserService::changeUserMoney(-$amount, $this->user_id, "提现-银行卡号".substr($bank->account, -4));
			UserWithdraw::create([
				'user_id' => $this->user_id,
				'bank_id' => $bank->id,
				'amount' => $amount,
				'order_sn' => Random::ordersn(),
				'real_amount' => $real_amount,
				'fee' => $fee
			]);
		} catch (\Exception $e) {
			$this->writeJson(0, $e->getMessage());
		}
		$this->writeJson();
	}
	
	#提现列表
	function withdrawList()
	{
		$rows = UserWithdraw::with(['bank'])->filter(function ($data){
			$data->bank_text = $data->bank->bank.'('.substr($data->bank->account, -4).')';
		})->where(['user_id' => $this->user_id])->order(['id'=>'desc'])->paginate()->items();
		$this->writeJson($rows);
	}
	
	#银行卡列表
	function bankList()
	{
		$rows = UserBank::where(['user_id' => $this->user_id])->order(['id'=>'desc'])->select();
		$this->writeJson($rows);
	}
}
